
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import twitter4j.Tweet;

public class DataProcessor {
	
	private ArrayList<Tweet> tweets;

	public static void main(String[] args) {
		Searcher s = new Searcher("hey", 10, 10);
		s.searchTweets();
		DataProcessor dp = new DataProcessor(s);
		int n = 5;
		String[] a = dp.nTopWords(5);
		for (int i=0;i<n;i++) {
			System.out.println(a[i]);
		}
		System.out.println(dp.numTweetsWithWord("hey"));
	}

	public DataProcessor(Searcher s) {
		tweets = (ArrayList<Tweet>) s.getResults();
	}

	public DataProcessor() {
		
	}
	
	
	public int numTweetsWithWord(String word) {
		int num = 0;
		word = "\\b" + word + "\\b";
		Pattern p = Pattern.compile(word, Pattern.CASE_INSENSITIVE);
		for (Tweet t: tweets) {
			Matcher m;
			StringTokenizer st = new StringTokenizer(t.getText());
			while (st.hasMoreTokens()) {
				m = p.matcher(st.nextToken().toLowerCase());
				if (m.matches()) {
					num++;
					break;
				}
			}
		}	
		return num;
	}

	public double pctTweetsWithWord(String word) {
		int num = numTweetsWithWord(word);
		double pct = (double)num/(double)tweets.size();
		return pct;
	}
	
	public Hashtable<Integer, String> wordsInTweets() {
		Hashtable<Integer, String> table = new Hashtable<Integer, String>(1000);
		Pattern p = Pattern.compile(".*\\p{Punct}.*");
		for (Tweet t: tweets) {
			StringTokenizer st = new StringTokenizer(t.getText());
			while (st.hasMoreTokens()) {
				String word = st.nextToken();
				word = word.toLowerCase();
				Matcher m = p.matcher(word);
				if (!m.matches()&&word!=null&&!word.contains("rt")) {
					table.put((Integer)word.hashCode(), word);
				}
			}
		}
		return table;
	}
	
	public Hashtable<Integer, String> locationsOfTweeters() {
		Hashtable<Integer, String> table = new Hashtable<Integer, String>(tweets.size());
		for (Tweet t: tweets) {
			table.put((Integer)t.getPlace().getName().hashCode(), t.getPlace().getName());
		}
		return table;
	}
	
	public String[] nTopWords(int n){
		Hashtable<Integer, String> table = wordsInTweets();
		Set<Integer> keys = table.keySet();
		String[] words = new String[n];
		for (Integer i:keys) {
			for (int k=0; k<n; k++) {
				String current = table.get(i);
				if (numTweetsWithWord(current)>numTweetsWithWord(words[k])){
					words[k] = table.get(i);
					break;
				}
			}
		}
		return words;
	}
}
